package com.jinghangzz.shop.common.util;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 多线程的工具类
 * @author TeaBig
 */
public class ExecutorUtil
{
	/* 线程池 */
	public static ExecutorService executorService ;
	
	static
	{
		/* 线程池 */
		executorService = Executors.newCachedThreadPool();
	}
	
	/**
	 * callable
	 * 不等待
	 */
	public static void submit(Callable callable)
	{
		executorService.submit(callable);
	}
	
	/**
	 * callable
	 * main方法等待
	 */
	public static void invokeAll(List<Callable<Object>> callList)
	{
		try
		{
			executorService.invokeAll(callList);
		} catch (InterruptedException e)
		{
			e.printStackTrace();
		}
	}
	
	/* 停止 */
	public static void stop()
	{
		executorService.shutdown();
	}
	
	public static void main(String[] args)
	{
		ConstatFinalUtil.SYS_LOGGER.info("==main开始==");
		/* 线程池 */
		ExecutorService executorService = Executors.newCachedThreadPool();
		/* 等待
		 *  V call() throws Exception;
		 *  */
//		executorService.submit( () -> 
//		{
//			Thread.sleep(5 * 1000);
//			ConstatFinalUtil.SYS_LOGGER.info("==submit==");
//			return null ;
//		}  );
		
		try
		{
			List<Callable<Object>> callList = new ArrayList<Callable<Object>>();
			callList.add( () -> 
			{
				Thread.sleep(5 * 1000);
				ConstatFinalUtil.SYS_LOGGER.info("==submit==");
				return null ;
			} ) ;
			
			executorService.invokeAll(callList);
		} catch (Exception e)
		{
			e.printStackTrace();
		}
		
		/* 停止服务 */
		executorService.shutdown();
		ConstatFinalUtil.SYS_LOGGER.info("==main结束==");
	}
}
